package com.fun.widget.tag.core;

import android.content.Context;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Typeface;
import android.support.annotation.NonNull;

/**
 * @author Created by AdminFun
 * @version v1.0.0
 * @date 2019/10/9 18:54
 * 标签ItemView样式构建器
 */
public class TagStyleBuilder implements Cloneable {

    private float textSize;                 // 文本字体大小
    private int textColor;                  // 文本颜色
    private int tagMaxLength;               // 文本最大长度
    private Typeface typeface;              // 文本样式

    private float borderWidth;              // 边框宽度
    private float borderRadius;             // 边框圆角
    private int borderColor;                // 边框颜色
    private boolean fillLine;               // 是否实线（反之则虚线）
    private int dottedLength;               // 虚线长度（仅fillLine=false时有效）
    private int dottedInterval;             // 虚线间距（仅fillLine=false时有效）
    private DashPathEffect pathEffect;      // 虚线参数

    private int backgroundColor;            // 背景颜色
    private int horizontalPadding;          // 标签内部横向间距
    private int verticalPadding;            // 标签内部纵向间距
    private int longPressTime = 500;        // 长按事件时间（毫秒，默认500）

    private int rippleDuration = 1000;      // 水波纹动画时间（毫秒，默认1000）
    private float rippleRadius;             // 水波纹半径
    private int rippleColor;                // 水波纹颜色
    private int rippleAlpha;                // 水波纹透明度

    private float distanceBaseLine;         // The distance between baseline and descent

    // DEFAULT

    /**
     * 设置默认的标签样式（内部标签样式，每一个样式对应一个独立的标签而不是标签容器）
     */
    public void reset(@NonNull Context context) {
        // text theme
        this.setTextSize(ScreenUtils.sp2px(context, 12));
        this.setTextColor(Color.parseColor("#333333"));
        this.setTagMaxLength(20);
        this.setTypeface(Typeface.DEFAULT);
        // border theme
        this.setBorderWidth(ScreenUtils.dip2px(context, 0.5f));
        this.setBorderRadius(ScreenUtils.dip2px(context, 15f));
        this.setBorderColor(Color.parseColor("#F3F4F5"));
        this.setFillLine(true);
        this.setDottedLength(ScreenUtils.dip2px(context, 2));
        this.setDottedInterval(ScreenUtils.dip2px(context, 1));
        // tag item background theme
        this.setBackgroundColor(Color.parseColor("#F3F4F5"));
        this.setHorizontalPadding(ScreenUtils.dip2px(context, 10));
        this.setVerticalPadding(ScreenUtils.dip2px(context, 3));
        this.setLongPressTime(500);
        // ripple theme
        this.setRippleDuration(1000);
        this.setRippleRadius(0f);
        this.setRippleColor(Color.parseColor("#EEEEEE"));
        this.setRippleAlpha(128);
        //
        this.setDistanceBaseLine(ScreenUtils.dip2px(context, 2.75f));
    }

    /**
     * 部分属性需要构建一下，但此方法并不需要用户调用，这是内部调用方法
     */
    public final void build() {
        if (fillLine) {
            this.pathEffect = null;
        } else {
            this.pathEffect = new DashPathEffect(new float[]{getDottedLength(), getDottedInterval()}, 5);
        }
    }

    @Override
    public TagStyleBuilder clone() {
        try {
            return (TagStyleBuilder) super.clone();
        } catch (CloneNotSupportedException e) {
            return this.copy();
        }
    }

    // SET

    public TagStyleBuilder setTextSize(float textSize) {
        this.textSize = textSize;
        return this;
    }

    public TagStyleBuilder setTextColor(int textColor) {
        this.textColor = textColor;
        return this;
    }

    public TagStyleBuilder setTagMaxLength(int tagMaxLength) {
        this.tagMaxLength = tagMaxLength;
        return this;
    }

    public TagStyleBuilder setTypeface(Typeface typeface) {
        this.typeface = typeface;
        return this;
    }

    public TagStyleBuilder setBorderWidth(float borderWidth) {
        this.borderWidth = borderWidth;
        return this;
    }

    public TagStyleBuilder setBorderRadius(float borderRadius) {
        this.borderRadius = borderRadius;
        return this;
    }

    public TagStyleBuilder setBorderColor(int borderColor) {
        this.borderColor = borderColor;
        return this;
    }

    public TagStyleBuilder setFillLine(boolean fillLine) {
        this.fillLine = fillLine;
        return this;
    }

    public TagStyleBuilder setDottedLength(int dottedLength) {
        this.dottedLength = dottedLength;
        return this;
    }

    public TagStyleBuilder setDottedInterval(int dottedInterval) {
        this.dottedInterval = dottedInterval;
        return this;
    }

    public TagStyleBuilder setBackgroundColor(int backgroundColor) {
        this.backgroundColor = backgroundColor;
        return this;
    }

    public TagStyleBuilder setHorizontalPadding(int horizontalPadding) {
        this.horizontalPadding = horizontalPadding;
        return this;
    }

    public TagStyleBuilder setVerticalPadding(int verticalPadding) {
        this.verticalPadding = verticalPadding;
        return this;
    }

    public TagStyleBuilder setLongPressTime(int longPressTime) {
        this.longPressTime = longPressTime;
        return this;
    }

    public TagStyleBuilder setRippleDuration(int rippleDuration) {
        this.rippleDuration = rippleDuration;
        return this;
    }

    public TagStyleBuilder setRippleRadius(float rippleRadius) {
        this.rippleRadius = rippleRadius;
        return this;
    }

    public TagStyleBuilder setRippleColor(int rippleColor) {
        this.rippleColor = rippleColor;
        return this;
    }

    public TagStyleBuilder setRippleAlpha(int rippleAlpha) {
        this.rippleAlpha = rippleAlpha;
        return this;
    }

    public TagStyleBuilder setDistanceBaseLine(float distanceBaseLine) {
        this.distanceBaseLine = distanceBaseLine;
        return this;
    }

    public TagStyleBuilder setPathEffect(DashPathEffect pathEffect) {
        this.pathEffect = pathEffect;
        return this;
    }

    // GET

    public float getTextSize() {
        return textSize;
    }

    public int getTextColor() {
        return textColor;
    }

    public int getTagMaxLength() {
        return tagMaxLength;
    }

    public Typeface getTypeface() {
        return typeface;
    }

    public float getBorderWidth() {
        return borderWidth;
    }

    public float getBorderRadius() {
        return borderRadius;
    }

    public int getBorderColor() {
        return borderColor;
    }

    public boolean isFillLine() {
        return fillLine;
    }

    public int getDottedLength() {
        return dottedLength;
    }

    public int getDottedInterval() {
        return dottedInterval;
    }

    public int getBackgroundColor() {
        return backgroundColor;
    }

    public int getHorizontalPadding() {
        return horizontalPadding;
    }

    public int getVerticalPadding() {
        return verticalPadding;
    }

    public int getLongPressTime() {
        return longPressTime;
    }

    public int getRippleDuration() {
        return rippleDuration;
    }

    public float getRippleRadius() {
        return rippleRadius;
    }

    public int getRippleColor() {
        return rippleColor;
    }

    public int getRippleAlpha() {
        return rippleAlpha;
    }

    public float getDistanceBaseLine() {
        return distanceBaseLine;
    }

    public DashPathEffect getPathEffect() {
        return pathEffect;
    }
    // copy

    public TagStyleBuilder copy() {
        return new TagStyleBuilder()
                .setTextSize(textSize)
                .setTextColor(textColor)
                .setTagMaxLength(tagMaxLength)
                .setTypeface(typeface)
                .setBorderWidth(borderWidth)
                .setBorderRadius(borderRadius)
                .setBorderColor(borderColor)
                .setFillLine(fillLine)
                .setDottedLength(dottedLength)
                .setDottedInterval(dottedInterval)
                .setBackgroundColor(backgroundColor)
                .setHorizontalPadding(horizontalPadding)
                .setVerticalPadding(verticalPadding)
                .setLongPressTime(longPressTime)
                .setRippleDuration(rippleDuration)
                .setRippleRadius(rippleRadius)
                .setRippleColor(rippleColor)
                .setRippleAlpha(rippleAlpha)
                .setDistanceBaseLine(distanceBaseLine)
                .setPathEffect(pathEffect);
    }
}